package com.brillilab.service.module.message.template;

import com.alibaba.fastjson.JSONObject;
import com.brillilab.common.rocketmq.OnsMessageVo;
import com.brillilab.domain.po.message.Message;
import com.brillilab.service.module.message.enums.AppPushTypeEnum;
import com.brillilab.service.module.message.enums.MessageTargetEnum;
import lombok.Data;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 各种消息的模板继承此类
 * 每种消息一个模板
 */
@Data
public abstract class AbstractMessageTemplate implements MessageTemplate{
    protected Long labId;
    protected Long labMemberId;
    protected Long receiverId;
    protected Integer type;
    protected String title;
    protected String content;
    protected String appContent;
    protected Long referId;
    protected String pushType= AppPushTypeEnum.NOTICE.getName();//推送类型默认为通知类型
    /**
     * key; userId
     * value: labMemberId
     */
    private Map<Long,Long> receiverMap;

    public AbstractMessageTemplate(Long labId,Long receiverId) {
        this.labId=labId;
        this.receiverId=receiverId;
    }

    public AbstractMessageTemplate(Long labId,Long receiverId,String pushType) {
        this.labId=labId;
        this.receiverId=receiverId;
        this.pushType=pushType;
    }

    @Override
    public Message buildMessage() {
        Assert.notNull(receiverId,"receiverId can not be null!");
        Message message=new Message();
        message.setLabId(labId);
        message.setReceiverId(receiverId);
        message.setLabMemberId(labMemberId);
        message.setType(type);
        message.setTitle(title);
        message.setContent(content);
        message.setData(getJsonData());
        message.setPushType(pushType);
        message.setReferId(referId);
        message.setTarget(MessageTargetEnum.CLIENT.getValue());
        if(receiverMap!=null){
            Set<Long> receiverIds=receiverMap.keySet();
            message.setReceiverIds(getReceiverIds());
        }
        return message;
    }

    @Override
    public List<Message> buildMessageList() {
        Assert.notNull(receiverMap,"receiverMap can not be null!");
        Set<Long> receiverIds=receiverMap.keySet();
        return receiverIds.stream().map(receiverId -> {
            Message message=new Message();
            message.setLabId(labId);
            message.setReceiverId(receiverId);
            message.setLabMemberId(receiverMap.get(receiverId));
            message.setType(type);
            message.setTitle(title);
            message.setContent(content);
            message.setData(getJsonData());
            message.setReferId(referId);
            message.setTarget(MessageTargetEnum.CLIENT.getValue());
            message.setReceiverIds(getReceiverIds());
            return message;
        }).collect(Collectors.toList());

    }

    private String getReceiverIds() {
        Set<Long> receiverIds=receiverMap.keySet();
        StringBuilder stringBuilder=new StringBuilder();
        receiverIds.forEach(e -> stringBuilder.append(e).append(","));
        String ids=stringBuilder.toString();
        return ids.substring(0,ids.length()-1);
    }

//    @Override
//    public OnsMessageVo buildOnsMessageVo(Long messageId) {
//        return new OnsMessageVo(OnsMessageEnum.PUSH_NOTICE.getType(), OnsMessageEnum.PUSH_NOTICE.getTitle(), messageId);
//    }

    @Override
    public OnsMessageVo buildOnsMessageVo(Long messageId, Integer type,String title) {
        return new OnsMessageVo(type, title, messageId);
    }

    @Override
    public Message buildMessageVo(String messageData) {
        if(StringUtils.isEmpty(messageData)){
            return null;
        }
        else{
            //反序列化消息字符
            Message message = JSONObject.parseObject(messageData, Message.class);
            return message;
        }
    }

    @Override
    public String getJsonData() {
        JSONObject object=new JSONObject();
        object.put("content",appContent);
        return object.toJSONString();
    }
}
